import * as actionTypes from "./constance";
import {
  getUserDetail,
  getUserAudio,
  getUserListenRank,
  getUserPlaylist,
  getUserFollows,
  getUserFans,
  getUserEvent,
  getCommentEvent,
  getUserImg,
} from "@/services/user";

const userDetail = (res) => ({
  type: actionTypes.GET_USERDETAIL,
  userDetailInfo: res,
});
const userImg = (res) => ({
  type: actionTypes.GET_USERIMG,
  userImgInfo: res.profile.avatarUrl,
});

const userAudio = (res) => ({
  type: actionTypes.GET_USERAUDIO,
  userAudioInfo: res,
});

const userListenRank = (res) => ({
  type: actionTypes.GET_USERLISTENRANK,
  userListenRankInfo: res,
});

const userPlaylist = (res) => ({
  type: actionTypes.GET_USERPLAYLIST,
  userPlaylistInfo: res,
});

const userFollows = (res) => ({
  type: actionTypes.GET_USERFOLLOWS,
  userFollowsInfo: res,
});
const userFans = (res) => ({
  type: actionTypes.GET_USERFANS,
  userFansInfo: res,
});
const userEvent = (res) => ({
  type: actionTypes.GET_USEREVENT,
  userEventInfo: res,
});
const commentEvent = (res) => ({
  type: actionTypes.GET_COMMENTEVENT,
  commentEventInfo: res,
});

const arr = [];
const id = [];

export const userDetailAction = (id) => {
  return (dispatch) => {
    getUserDetail(id).then((res) => {
      dispatch(userDetail(res));
    });
  };
};
export const userImgAction = (id) => {
  return (dispatch) => {
    getUserImg(id).then((res) => {
      dispatch(userImg(res));
    });
  };
};

export const userAudioAction = (uid) => {
  return (dispatch) => {
    getUserAudio(uid).then((res) => {
      dispatch(userAudio(res));
    });
  };
};

export const userListenRankAction = (uid, type) => {
  return (dispatch) => {
    getUserListenRank(uid, type).then((res) => {
      dispatch(userListenRank(res));
    });
  };
};

export const userPlaylistAction = (uid, limit, offset) => {
  return (dispatch) => {
    getUserPlaylist(uid, limit, offset).then((res) => {
      dispatch(userPlaylist(res));
    });
  };
};

export const userFollowsAction = (uid, limit, offset) => {
  return (dispatch) => {
    getUserFollows(uid, limit, offset).then((res) => {
      dispatch(userFollows(res));
    });
  };
};
export const userFansAction = (uid, limit, offset) => {
  return (dispatch) => {
    getUserFans(uid, limit, offset).then((res) => {
      dispatch(userFans(res));
    });
  };
};
export const userEventAction = (uid, limit, lasttime) => {
  // ("在action中了",arr)
  return (dispatch) => {
    getUserEvent(uid, limit, lasttime).then((res) => {
      // 点击到另一个用户清空所有
      if (lasttime === -1) {
        arr.splice(0, arr.length);
        id.splice(0, id.length);
      }
      // 保证是同一个id
      arr.push(res);
      dispatch(userEvent(arr))(arr);
    });
  };
};

export const userCommentEventAction = (id) => {
  return (dispatch) => {
    getCommentEvent(id).then((res) => {
      dispatch(commentEvent(res));
    });
  };
};
